package oto

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"time"
)

// Build 输入另一个后端的ip(例如114.116.227.230:13124)和你想要使用的端口（记得打开你端口的安全权限,如9999）
func (r *gusinx) Build(ip string, port string) {
	r.ip = ip
	r.R = gin.Default()
	r.port = port
	r.R.ForwardedByClientIP = true

	r.R.SetTrustedProxies([]string{"http://" + r.ip})
	r.R.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"http://" + r.ip}, // 白名单
		AllowMethods:     []string{"GET", "POST", "PUT", "PATCH"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Authorization"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}))
}

// Init 输入另一个后端的ip(例如114.116.227.230:13124)和你想要使用的端口（记得打开你端口的安全权限,如9999）,再一次性声明密钥
func (r *gusinx) Init(ip string, port string, keys string) error {
	r.ip = ip
	r.R = gin.Default()
	r.port = port
	err := set32Key(keys)
	if err != nil {
		return err
	}

	r.R.ForwardedByClientIP = true

	r.R.SetTrustedProxies([]string{"http://" + r.ip})
	r.R.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"http://" + r.ip}, // 白名单
		AllowMethods:     []string{"GET", "POST", "PUT", "PATCH"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Authorization"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}))
	return nil
}
func (r *gusinx) Serve() {
	r.R.Run(":" + r.port)
}
